;(function(){
    "use strict"

    const osubmit = document.getElementById("submit");
    const otel = document.getElementById("tel");
    const opassword = document.getElementById("password");
    
    osubmit.onclick = function(){
        ajax("http://localhost:3000/api",function(res){
            // console.log(res);
            res = JSON.parse(res);
            console.log(res)
            if(res.code === 0){
                alert(res.title);
                otel.value = "";
                otel.focus();
            }else if(res.code === 1){
                if(confirm("注册成功，点击跳转到登录")){
                    location.href = "./login.html";
                }
            }
    
        },{
            type:"register",
            username:otel.value,
            password:opassword.value
        })
    }
    
    
    function ajax(url, callback, data){
        let str = "";
        for(let i in data){
            str += `${i}=${data[i]}&`
        }
        url += "?" + str;
        const xhr = new XMLHttpRequest();
        xhr.open("get",url)
        xhr.onload = function(){
            if(xhr.status === 200){
                callback(xhr.responseText)
            }
        }
        xhr.send();
    }


})();